﻿using System;
using System.Diagnostics;

namespace ProgramMain.OpticNet
{
    public partial class FrmDocumentList : ProgramMain.Framework.frmBase
    {
        public FrmDocumentList()
        {
            InitializeComponent();
        }
        public string DocDirecory
        {
            get { return Environment.GetFolderPath(Environment.SpecialFolder.Personal); }
        }

        public int? TagID { set { documentList1.TagID = value; } }
        public int? VertexID { set { documentList1.VertexID = value; } }
        public int? CableTunnelID { set { documentList1.CableTunnelID = value; } }
        public int? CurveElementID { set { documentList1.CurveElementID = value; } }

        private void FrmDocumentList_Load(object sender, EventArgs e)
        {
            documentList1.RefreshControl();
        }

        private void radioGroup1_EditValueChanged(object sender, EventArgs e)
        {
            txtFile.Visible = (int)radioGroup1.EditValue == 1;
        }

        private void SaveFile()
        {
            if (!Framework.BaseSecurity.CheckSecurityTechAction()) return;
            if (documentList1.Document != null)
            {
                saveFileDialog1.FileName = txtFile.Text;
                byte[] content = documentList1.GetDocumentContent(documentList1.Document.ID);
                if (content != null)
                {
                    using (System.IO.Stream st = saveFileDialog1.OpenFile())
                    {
                        st.Write(content, 0, content.Length);
                        st.Close();
                    }
                }
            }
        }

        private void OpenFile()
        {
            if (!Framework.BaseSecurity.CheckSecurityTechAction()) return;
            if (documentList1.Document != null)
            {
                txtFile.Text = DocDirecory + documentList1.Document.FileName;
                saveFileDialog1.FileName = @"C:\Windows\Temp\" + documentList1.Document.FileName;
                if (System.IO.File.Exists(saveFileDialog1.FileName))
                    System.IO.File.Delete(saveFileDialog1.FileName);
                byte[] content = documentList1.GetDocumentContent(documentList1.Document.ID);
                if (content != null)
                {
                    using (System.IO.Stream st = saveFileDialog1.OpenFile())
                    {
                        st.Write(content, 0, content.Length);
                        st.Close();

                        Process process = new Process();
                        process.StartInfo.FileName = saveFileDialog1.FileName;
                        process.Start();
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if ((int)radioGroup1.EditValue == 1)
                SaveFile();
            if ((int)radioGroup1.EditValue == 0)
                OpenFile();
        }

        private void documentList1_DocumentChanged(object sender, EventArgs e)
        {
            txtFile.Text = documentList1.Document != null && !string.IsNullOrEmpty(documentList1.Document.FileName)
                               ? DocDirecory +"\\" + documentList1.Document.FileName
                               : string.Empty;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void txtFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if((string)e.Button.Tag== "save")
            {
                saveFileDialog1.FileName= txtFile.Text;
                if(saveFileDialog1.ShowDialog( )== System.Windows.Forms.DialogResult.OK)
                    txtFile.Text = saveFileDialog1.FileName;
            }
        }
    }
}
